﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYPlumbingAutoDesign : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				UIApplication application = commandData.Application;
				Autodesk.Revit.ApplicationServices.Application application2 = application.Application;
				Document document = application.ActiveUIDocument.Document;
				Selection selection = commandData.Application.ActiveUIDocument.Selection;
				ViewPlan viewPlan = document.ActiveView as ViewPlan;
				if (viewPlan == null || (int)viewPlan.ViewType != 1)
				{
					string text = "请在楼层平面下运行该命令！";
					YJKMessageBox.Show(text);
					result = Autodesk.Revit.UI.Result.Failed;
				}
				else
				{
					string text2 = "请选择排水系统设计需要的实体：一根水管立管、若干卫生设备……";
					IList<Element> list = null;
					transaction = new Transaction(document, "排水系统自动连接设计");
					transaction.Start();
					PlumbingAutoDesignLogic plumbingAutoDesignLogic;
					for (;;)
					{
						list = new List<Element>();
						if (selection.GetElementIds().Count > 0)
						{
							foreach (ElementId elementId in selection.GetElementIds())
							{
								list.Add(document.GetElementById(elementId));
							}
							plumbingAutoDesignLogic = new PlumbingAutoDesignLogic(list);
							string text = plumbingAutoDesignLogic.ExamineElements();
							if (!(text != ""))
							{
								break;
							}
							YJKMessageBox.Information(text);
							RevitVersionFuncs.ClearSelection(selection);
						}
						else
						{
							list = selection.PickElementsByRectangle(new PlumbingDesignFilter(), text2);
							plumbingAutoDesignLogic = new PlumbingAutoDesignLogic(list);
							string text = plumbingAutoDesignLogic.ExamineElements();
							if (!(text != ""))
							{
								break;
							}
							YJKMessageBox.Information(text);
							RevitVersionFuncs.ClearSelection(selection);
						}
					}
					if (plumbingAutoDesignLogic.CreatHorizontalPipe())
					{
						transaction.Commit();
					}
					else
					{
						transaction.RollBack();
					}
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
